package com.example.user.myhjjf_app.view;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.PopupMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.user.myhjjf_app.HJJF_Url;
import com.example.user.myhjjf_app.R;
import com.example.user.myhjjf_app.model.Model_Content;
import com.example.user.myhjjf_app.model.Model_Regional;
import com.example.user.myhjjf_app.presenter.Implementation_operation;
import com.example.user.myhjjf_app.presenter.SiteOperation;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class Activity_flxAdd extends AppCompatActivity {

    @BindView(R.id.btn_back)
    LinearLayout btnBack;
    @BindView(R.id.tx_title)
    TextView txTitle;
    @BindView(R.id.btn_edit)
    LinearLayout btnEdit;
    @BindView(R.id.add_mc)
    EditText addMc;
    @BindView(R.id.textView)
    TextView textView;
    @BindView(R.id.add_bm)
    EditText addBm;
    @BindView(R.id.add_cj)
    EditText addCj;
    @BindView(R.id.textView2)
    TextView textView2;
    @BindView(R.id.add_xh)
    EditText addXh;
    @BindView(R.id.add_qysj)
    EditText addQysj;
    @BindView(R.id.xz)
    Button xz;
    @BindView(R.id.qr)
    Button qr;
    SiteOperation siteOperation;
    String type;
    Model_Content.ValueBean bean;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flx_add);
        ButterKnife.bind(this);
        Bundle bundle = getIntent().getExtras();
        siteOperation = new Implementation_operation(this);
        type = bundle.getString("type");
        if (!type.equals("add")){
            bean = (Model_Content.ValueBean) bundle.getSerializable("bean");
            addMc.setText(bean.getTitle());
            addBm.setText(bean.getCode());
            addCj.setText(bean.getManufactor());
            addXh.setText(bean.getModel());
            addQysj.setText(bean.getStart_date());
            if (type.equals("select")) {
                txTitle.setText("防雷箱详情");
                addMc.setEnabled(false);
                addBm.setEnabled(false);
                addCj.setEnabled(false);
                addXh.setEnabled(false);
                qr.setVisibility(View.GONE);
                xz.setVisibility(View.GONE);
            }
        }else {
            txTitle.setText("添加防雷箱");
        }
    }

    @OnClick({R.id.btn_back, R.id.btn_edit, R.id.xz, R.id.qr})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_edit:
                showMenu(btnEdit);
                break;
            case R.id.xz:
                Calendar calendar = Calendar.getInstance();
                if (!addQysj.getText().toString().equals("")) {
                    try {
                        calendar.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(addQysj.getText().toString()));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }
                int year = calendar.get(Calendar.YEAR);
                int monthOfYear = calendar.get(Calendar.MONTH);
                int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
                new DatePickerDialog(Activity_flxAdd.this, dateSetListener, year, monthOfYear, dayOfMonth).show();
                break;
            case R.id.qr:
                siteOperation = new Implementation_operation(Activity_flxAdd.this);
                HashMap map = new HashMap();
                String url = HJJF_Url.getHJJF_Url().login("api/data/flx_add.do");
                if (type.equals("select")) {
                    url = HJJF_Url.getHJJF_Url().login("api/data/flx_edit.do");
                    map.put("id", bean.getId());
                }
                map.put("title", addMc.getText().toString());
                map.put("code", addBm.getText().toString());
                map.put("zoneId", Model_Regional.getRegional().getZoneId());
                map.put("siteId", Model_Regional.getRegional().getSiteId());
                map.put("departId", Model_Regional.getRegional().getDepartId());
                map.put("manufactor", addCj.getText().toString());
                map.put("model", addXh.getText().toString());
                map.put("startDate", addQysj.getText().toString());
                Iterator iterator = map.entrySet().iterator();
                int i = 0;
                while (iterator.hasNext()){
                    Map.Entry entry = (Map.Entry) iterator.next();
                    if (entry.getValue().equals("")){
                        Toast.makeText(this, "所有选项不能为空~~", Toast.LENGTH_SHORT).show();
                        i=0;
                        break;
                    }else {
                        i=1;
                    }
                }
                if (i==1){
                    siteOperation.Add(map,url);
                }
                break;
        }
    }

    private DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            String str = year + "-";
            if (monthOfYear + 1 >= 10) {
                str += (monthOfYear + 1) + "-";
            } else {
                str += "0" + (monthOfYear + 1) + "-";
            }
            if (dayOfMonth >= 10) {
                addQysj.setText(str + dayOfMonth);
            } else {
                addQysj.setText(str + "0" + dayOfMonth);
            }
        }
    };
    private void showMenu(View view){
        // View当前PopupMenu显示的相对View的位置
        PopupMenu popupMenu = new PopupMenu(this,view);
        //menu布局
        popupMenu.getMenuInflater().inflate(R.menu.menu,popupMenu.getMenu());
        //点击事件
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()){
                    case R.id.btn_menu_edit:
                        txTitle.setText("防雷箱编辑");
                        addMc.setEnabled(true);
                        addBm.setEnabled(true);
                        addCj.setEnabled(true);
                        addXh.setEnabled(true);
                        qr.setVisibility(View.VISIBLE);
                        xz.setVisibility(View.VISIBLE);
                        break;
                    case R.id.btn_menu_delete:
                        String url = HJJF_Url.getHJJF_Url().login("api/data/flx_drop.do");
                        siteOperation.delete(url,bean.getId());
                        break;
                }
                return false;
            }
        });
        //关闭事件
        popupMenu.setOnDismissListener(new PopupMenu.OnDismissListener() {
            @Override
            public void onDismiss(PopupMenu menu) {

            }
        });
        popupMenu.show();
    }
}
